//
// File: det.cpp
//
// MATLAB Coder version            : 5.1
// C/C++ source code generated on  : 22-Feb-2021 03:11:30
//

// Include Files
#include "det.h"
#include "rt_nonfinite.h"
#include <cmath>
#include <cstring>

// Function Definitions
//
// Arguments    : const double x[9]
// Return Type  : double
//
namespace coder
{
  double b_det(const double x[9])
  {
    double b_x[9];
    double y;
    int jA;
    int jy;
    signed char ipiv[3];
    boolean_T isodd;
    std::memcpy(&b_x[0], &x[0], 9U * sizeof(double));
    ipiv[0] = 1;
    ipiv[1] = 2;
    for (int j = 0; j < 2; j++) {
      double smax;
      int b_tmp;
      int i;
      int ix;
      int jp1j;
      int k;
      int mmj_tmp;
      mmj_tmp = 1 - j;
      b_tmp = j << 2;
      jp1j = b_tmp + 2;
      jy = 3 - j;
      jA = 0;
      ix = b_tmp;
      smax = std::abs(b_x[b_tmp]);
      for (k = 2; k <= jy; k++) {
        double s;
        ix++;
        s = std::abs(b_x[ix]);
        if (s > smax) {
          jA = k - 1;
          smax = s;
        }
      }

      if (b_x[b_tmp + jA] != 0.0) {
        if (jA != 0) {
          jy = j + jA;
          ipiv[j] = static_cast<signed char>(jy + 1);
          smax = b_x[j];
          b_x[j] = b_x[jy];
          b_x[jy] = smax;
          smax = b_x[j + 3];
          b_x[j + 3] = b_x[jy + 3];
          b_x[jy + 3] = smax;
          smax = b_x[j + 6];
          b_x[j + 6] = b_x[jy + 6];
          b_x[jy + 6] = smax;
        }

        i = (b_tmp - j) + 3;
        for (jy = jp1j; jy <= i; jy++) {
          b_x[jy - 1] /= b_x[b_tmp];
        }
      }

      jy = b_tmp + 3;
      jA = b_tmp;
      for (k = 0; k <= mmj_tmp; k++) {
        smax = b_x[jy];
        if (b_x[jy] != 0.0) {
          ix = b_tmp + 1;
          i = jA + 5;
          jp1j = (jA - j) + 6;
          for (int ijA = i; ijA <= jp1j; ijA++) {
            b_x[ijA - 1] += b_x[ix] * -smax;
            ix++;
          }
        }

        jy += 3;
        jA += 3;
      }
    }

    isodd = (ipiv[0] > 1);
    y = b_x[0] * b_x[4] * b_x[8];
    if (ipiv[1] > 2) {
      isodd = !isodd;
    }

    if (isodd) {
      y = -y;
    }

    return y;
  }

  //
  // Arguments    : const double x[16]
  // Return Type  : double
  //
  double det(const double x[16])
  {
    double b_x[16];
    double y;
    int jA;
    int jy;
    signed char ipiv[4];
    boolean_T isodd;
    std::memcpy(&b_x[0], &x[0], 16U * sizeof(double));
    ipiv[0] = 1;
    ipiv[1] = 2;
    ipiv[2] = 3;
    for (int j = 0; j < 3; j++) {
      double smax;
      int b_tmp;
      int i;
      int ix;
      int jp1j;
      int k;
      int mmj_tmp;
      mmj_tmp = 2 - j;
      b_tmp = j * 5;
      jp1j = b_tmp + 2;
      jy = 4 - j;
      jA = 0;
      ix = b_tmp;
      smax = std::abs(b_x[b_tmp]);
      for (k = 2; k <= jy; k++) {
        double s;
        ix++;
        s = std::abs(b_x[ix]);
        if (s > smax) {
          jA = k - 1;
          smax = s;
        }
      }

      if (b_x[b_tmp + jA] != 0.0) {
        if (jA != 0) {
          jy = j + jA;
          ipiv[j] = static_cast<signed char>(jy + 1);
          smax = b_x[j];
          b_x[j] = b_x[jy];
          b_x[jy] = smax;
          smax = b_x[j + 4];
          b_x[j + 4] = b_x[jy + 4];
          b_x[jy + 4] = smax;
          smax = b_x[j + 8];
          b_x[j + 8] = b_x[jy + 8];
          b_x[jy + 8] = smax;
          smax = b_x[j + 12];
          b_x[j + 12] = b_x[jy + 12];
          b_x[jy + 12] = smax;
        }

        i = (b_tmp - j) + 4;
        for (jy = jp1j; jy <= i; jy++) {
          b_x[jy - 1] /= b_x[b_tmp];
        }
      }

      jy = b_tmp + 4;
      jA = b_tmp;
      for (k = 0; k <= mmj_tmp; k++) {
        smax = b_x[jy];
        if (b_x[jy] != 0.0) {
          ix = b_tmp + 1;
          i = jA + 6;
          jp1j = (jA - j) + 8;
          for (int ijA = i; ijA <= jp1j; ijA++) {
            b_x[ijA - 1] += b_x[ix] * -smax;
            ix++;
          }
        }

        jy += 4;
        jA += 4;
      }
    }

    isodd = (ipiv[0] > 1);
    if (ipiv[1] > 2) {
      isodd = !isodd;
    }

    y = b_x[0] * b_x[5] * b_x[10] * b_x[15];
    if (ipiv[2] > 3) {
      isodd = !isodd;
    }

    if (isodd) {
      y = -y;
    }

    return y;
  }
}

//
// File trailer for det.cpp
//
// [EOF]
//
